﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MinhUtils;

namespace StyleHelper
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            txtHTML.KeyDown += new KeyEventHandler(FormUtil.TextBoxControlAHanlder);
            txtCSS.KeyDown += new KeyEventHandler(FormUtil.TextBoxControlAHanlder);
        }

        private void btnCSSInit_Click(object sender, EventArgs e)
        {
            InitCSS();
        }

        private void InitCSS()
        {
            HashSet<string> hsCssClass = new HashSet<string>();

            string htmlText = txtHTML.Text;
            StringBuilder cssText = new StringBuilder();

            string before = "class[^=]*=[^\"]*\"";
            string pattern = "[^\"]+"; //match class{space}={space}"className"
            string after = "\"";

            foreach (Match match in RegexHelper.MatchBeforeAfter(htmlText, before, after, pattern))
            {
                string[] classNames = match.Value.Split(' ');

                foreach (string className in classNames)
                {
                    if (hsCssClass.Contains(className) == false)
                    {
                        hsCssClass.Add(className);
                        cssText.Append("." + className + "{");
                        cssText.Append(StringUtil.TextboxNewLine);
                        cssText.Append(StringUtil.TextboxTab + "text-transform: uppercase;");
                        cssText.Append(StringUtil.TextboxNewLine);
                        cssText.Append("}");
                        cssText.Append(StringUtil.TextboxNewLine);
                        cssText.Append(StringUtil.TextboxNewLine);
                    }
                }
            }

            txtCSS.Text = cssText.ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtHTML.Text = string.Empty;
            txtCSS.Text = string.Empty;
        }
    }
}
